package com.framework.base.interceptor;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.framework.base.constant.WebConstants;
import com.framework.base.struts.BaseVo;
import com.framework.base.util.Struts2Utils;
import com.framework.main.sys.dept.dao.DeptDao;
import com.framework.main.sys.dept.domain.DeptVO;
import com.framework.main.sys.user.dao.UserDao;
import com.framework.main.sys.user.domain.SysDataAuthority;
import com.framework.main.sys.user.domain.SysUserRoleVO;
import com.framework.main.sys.user.domain.UserVo;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * 用于获取数据权限
 * @author www.51wuxia.com
 *
 */
public class DataAuthorityAbstractInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 9170936534167917368L;
	private HashMap<String,Object> queryMap; //获取查询参数
	private UserDao userDao;
	private DeptDao deptDao;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpSession session = Struts2Utils.getSession();
		HttpServletRequest request = Struts2Utils.getRequest();
		String menuId = request.getParameter("queryMap.menuid");//获取菜单Id
		String toAction = invocation.getProxy().getActionName();//获取提交的Action地址
		UserVo uvo = (UserVo) session.getAttribute(WebConstants.CURRENTUSER);//获取登录用户
		if(menuId==null || menuId.equals("")||toAction.indexOf("toList")<=-1||uvo.getIdf().equals("88888888")){
			return invocation.invoke();
		}
		//获取Action值栈
		ValueStack stack = invocation.getInvocationContext().getValueStack();
		queryMap = (HashMap<String, Object>) stack.getContext().get("queryMap");
		if(queryMap==null){
			queryMap = new HashMap<String,Object>();
		}
		List<BaseVo> list = this.getUserDao().queryForList("toListUserRoleByUserID",uvo.getIdf());
		if(list.size()>0){//判断该用户是否被分配角色
			SysUserRoleVO uro = (SysUserRoleVO) list.get(0);
			SysDataAuthority davo = new SysDataAuthority();
			davo.setMenuIdf(menuId);
			davo.setRoleIdf(uro.getRoleIdf());
			List<BaseVo> data = this.getUserDao().queryForList("toGetDataScopef", davo);//查找该角色的数据范围
			List<BaseVo> depts = this.getDeptDao().queryForList("toListLogin", uvo.getIdf());//查找该用户部门
			SysDataAuthority scope = (SysDataAuthority) data.get(0); 
			if(scope.getDataScopef()==1){
				if(depts.size()>0){
					DeptVO dept = (DeptVO) depts.get(0);
					this.queryMap.put("deptidf",dept.getIdf());
				}else{
					this.queryMap.put("deptidf","");
				}
			}
			if(scope.getDataScopef()==2){
				this.queryMap.put("createidf",uvo.getIdf());
			}
			stack.setValue("queryMap",queryMap);
			invocation.getInvocationContext().setValueStack(stack);
		}
		return invocation.invoke();
	}
	
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public DeptDao getDeptDao() {
		return deptDao;
	}

	public void setDeptDao(DeptDao deptDao) {
		this.deptDao = deptDao;
	}
	
}
